package com.yuandian.bpm.bi.modules.quartz.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.bpm.admin.api.entity.SysDictItem;
import com.yuandian.bpm.bi.modules.quartz.enums.TaskCronEnum;
import com.yuandian.bpm.bi.modules.quartz.enums.TaskStateEnum;
import com.yuandian.bpm.bi.modules.quartz.service.ConfReportTaskInfoService;
import com.yuandian.bpm.bi.modules.quartz.service.impl.HandelDeleteDataTaskServiceImpl;
import com.yuandian.bpm.bi.modules.quartz.service.impl.HandelHeartbeatTaskServiceImpl;
import com.yuandian.bpm.bi.modules.quartz.vo.ConfReportRunningTaskVo;
import com.yuandian.bpm.bi.modules.quartz.vo.ConfReportTaskInfo;
import com.yuandian.bpm.bi.modules.quartz.vo.ConfReportTaskLog;
import com.yuandian.bpm.bi.modules.quartz.vo.OptionVo;
import com.yuandian.bpm.common.core.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 报表定时任务表-Controller
 * @author zfsn
 * @date 2023-6-19
 */
@Api(tags = "报表定时任务表功能接口")
@RequestMapping("/reportTaskInfo")
@RestController
@Slf4j
public class ConfReportTaskInfoController {

    @Autowired
    private ConfReportTaskInfoService confReportTaskInfoService;


	@Autowired
	private HandelHeartbeatTaskServiceImpl handelHeartbeatTaskService;

	@Autowired
	private HandelDeleteDataTaskServiceImpl deleteDataTaskService;



	 /**
	  * 获取任务信息
	  * @param id 主键
	  * @return 通用返回对象
	  */
	 @GetMapping("/getTaskInfoById/{id}")
	 @ApiOperation(value = "根据任务ID查询任务详情信息", notes = "根据任务ID查询任务详情信息")
	 public R<ConfReportTaskInfo> getTaskInfoById(@PathVariable int id) {
		 ConfReportTaskInfo param = new ConfReportTaskInfo();
		 param.setId(id);
		 return confReportTaskInfoService.getTaskInfoById(param);
	 }


	/**
	 * 分页查询
	 * @param confReportTaskInfo 筛选条件
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "分页查询-任务信息", notes = "分页查询-任务信息")
	@PostMapping("/getTaskInfoList")
	public R<Page<ConfReportTaskInfo>> getTaskInfoList(@RequestBody ConfReportTaskInfo confReportTaskInfo) {
		Page<ConfReportTaskInfo> page = null;
		if (confReportTaskInfo.getPageNum() > 0 && confReportTaskInfo.getPageSize() > 0) {
			page = new Page<>(confReportTaskInfo.getPageNum(), confReportTaskInfo.getPageSize());
		}

		return R.ok(confReportTaskInfoService.getTaskInfoList(page, confReportTaskInfo));
	}


	/**
	 * 分页查询-执行中的任务
	 * @param confReportTaskInfo 筛选条件
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "分页查询-执行中的任务", notes = "分页查询-执行中的任务")
	@PostMapping("/getRunningTaskList")
	public R<Page<ConfReportRunningTaskVo>> getRunningTaskList(@RequestBody ConfReportTaskInfo confReportTaskInfo) {
		Page<ConfReportRunningTaskVo> page = null;
		if (confReportTaskInfo.getPageNum() > 0 && confReportTaskInfo.getPageSize() > 0) {
			page = new Page<>(confReportTaskInfo.getPageNum(), confReportTaskInfo.getPageSize());
		}

		return R.ok(confReportTaskInfoService.getRunningTaskList(page, confReportTaskInfo));
	}


	/**
	 * 分页查询日志信息-任务明细
	 * @param confReportTaskInfo 筛选条件
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "分页查询日志信息-任务明细", notes = "分页查询日志信息-任务明细")
	@PostMapping("/getTaskLogList")
	public R<Page<ConfReportTaskLog>> getTaskLogList(@RequestBody ConfReportTaskInfo confReportTaskInfo) {
		Page<ConfReportTaskLog> page = null;
		if (confReportTaskInfo.getPageNum() > 0 && confReportTaskInfo.getPageSize() > 0) {
			page = new Page<>(confReportTaskInfo.getPageNum(), confReportTaskInfo.getPageSize());
		}

		return R.ok(confReportTaskInfoService.getTaskLogList(page, confReportTaskInfo));
	}


		/**
		 * 新增任务
		 * @param confReportTaskInfo 实例对象
		 * @return 通用返回对象
		 */
	@ApiOperation(value = "新增任务", notes = "新增数据")
    @PostMapping("/saveTaskInfo")
    public R<Object> saveTaskInfo(@RequestBody ConfReportTaskInfo confReportTaskInfo) {
        return confReportTaskInfoService.saveTaskInfo(confReportTaskInfo);
    }


		/**
		 * 修改任务
		 * @param confReportTaskInfo 实例对象
		 * @return 通用返回对象
		 */
	@ApiOperation(value = "修改任务", notes = "修改任务")
    @PostMapping("/updateTaskInfo")
    public R<Object> updateTaskInfo(@RequestBody ConfReportTaskInfo confReportTaskInfo) {
        return confReportTaskInfoService.updateTaskInfoById(confReportTaskInfo);
    }


		/**
		 * 通过主键删除数据
		 * @param ids 主键
		 * @return 通用返回对象
		 */
	@ApiOperation(value = "通过主键删除数据", notes = "通过主键删除数据")
    @DeleteMapping("/deleteTaskByIds/{ids}")
    public R<Object> deleteTaskByIds(@PathVariable List<Integer> ids){
    	ConfReportTaskInfo param = new ConfReportTaskInfo();
    	param.setIds(ids);
    	param.setTaskState(TaskStateEnum.RUNNING.getCode());
        return confReportTaskInfoService.deleteTaskByIds(param);
    }

		/**
		 * 暂停任务
		 * @param ids 主键
		 * @return 通用返回对象 suspend
		 */
	@ApiOperation(value = "暂停任务", notes = "暂停任务")
	@PutMapping("/stopTaskByIds/{ids}")
	public R<Object> suspendTaskByIds(@PathVariable List<Integer> ids) {
		if (CollectionUtils.isEmpty(ids)) { return R.failed("任务ID不能为空"); }
		return confReportTaskInfoService.suspendTaskByIds(ids, TaskStateEnum.SUSPEND.getCode());
	}



	@ApiOperation(value = "心跳任务", notes = "心跳任务")
	@PutMapping("/handelHeartbeatTask")
	public void handelHeartbeatTask() {
		handelHeartbeatTaskService.handleData(new ConfReportTaskInfo(), null);
		deleteDataTaskService.handleData(new ConfReportTaskInfo(), null);
	}


	/**
	 * 启动任务
	 * @param ids 主键
	 * @return 通用返回对象 suspend
	 */
	@ApiOperation(value = "启动任务", notes = "启动任务")
	@PutMapping("/startTaskByIds/{ids}")
	public R<Object> startTaskByIds(@PathVariable List<Integer> ids) {
		if (CollectionUtils.isEmpty(ids)) { return R.failed("任务ID不能为空"); }
		return confReportTaskInfoService.startTaskByIds(ids, TaskStateEnum.RUNNING.getCode());
	}


	/**
	 * 全局配置-运行失败提醒-平台提醒
	 * @param state 状态 0：不勾选平台提醒  1：勾选平台提醒
	 * @return 通用返回对象 suspend
	 */
	@ApiOperation(value = "全局配置-运行失败提醒-平台提醒", notes = "启动任务")
	@PutMapping("/platformReminderHandle/{state}")
	public R<Object> platformReminderHandle(@PathVariable Integer state) {
		return confReportTaskInfoService.platformReminderHandle(state);
	}


	/**
	 * 全局配置-运行失败提醒-平台提醒
	 * @return 通用返回对象 suspend
	 */
	@ApiOperation(value = "获取全局配置-运行失败提醒-平台提醒", notes = "启动任务")
	@GetMapping("/getPlatformReminderState")
	public R<Object> getPlatformReminderState() {
		return confReportTaskInfoService.getPlatformReminderState();
	}

	/**
	 * 获取 cron 表达式下拉框
	 * @return
	 */
	@ApiOperation(value = "获取 cron 表达式下拉框", notes = "获取 cron 表达式下拉框")
	@GetMapping("/getCronList")
	public R<Object> getCronList() {
		List<OptionVo> list = new ArrayList<>();
		R<List<SysDictItem>> dictByType = null;
/*
		try {
			long sTime = System.currentTimeMillis();
			dictByType = remoteDictService.getDictByType("ops_report_task_cron");
			log.info("[自定义报表]-[远程调用-后台配置服务]-[获取字典数据]-耗时: {} MS", System.currentTimeMillis() - sTime);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("[自定义报表]-[远程调用-后台配置服务]-[获取字典数据]-报错: {} ", e.getMessage());
		}

		if (dictByType != null && dictByType.getData() != null) {
			for (SysDictItem sysDictItem : dictByType.getData()) {
				OptionVo vo = new OptionVo();
				vo.setLabel(sysDictItem.getLabel());
				vo.setValue(sysDictItem.getItemValue());
				list.add(vo);
			}
		}
*/
		for (TaskCronEnum cronType : TaskCronEnum.values()) {
			OptionVo vo1 = new OptionVo();
			vo1.setValue(cronType.getCronStr());
			vo1.setLabel(cronType.getDescription());
			list.add(vo1);
		}

		return R.ok(list);
	}

}
